<template>
  <el-dialog
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="dialogData.title"
    :visible.sync="isDialog"
    width="1050px"
  >
    <el-form
      ref="submitFormRef"
      :model="submitForm"
      :rules="submitFormRules"
      label-position="right"
      label-width="auto"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="submitForm.title"
          clearable
          disabled
          placeholder="请输入标题"
          type="text"
        ></el-input>
      </el-form-item>

      <el-form-item label="文章内容" prop="content">
        <editor
          id="ah"
          :content="submitForm.content"
          @changed="
            (value) => {
              submitForm.content = value;
            }
          "
        ></editor>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        :loading="isLoading.submit"
        type="primary"
        @click="handleSubmit"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { edit, info } from "@/api/article";
import editor from "@/components/editor.vue";

export default {
  name: "Edit",
  components: { editor },
  props: {
    info: {
      type: Object,
    },
  },
  data() {
    return {
      // 表单提交
      submitForm: {
        article_id: "",
        title: "",
        content: "",
        sort: 0,
      },
      // 表单验证
      submitFormRules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [
          { required: true, message: "请选择发布时间", trigger: "blur" },
        ],
      },
      // 数据
      dialogData: {
        title: "",
      },
      // 加载状态
      isLoading: {
        submit: false,
      },
      // 是否弹窗
      isDialog: true,
    };
  },
  created() {
    this.dialogData.title = "修改" + "(" + this.info.title + ")";
    this.getData();
  },
  methods: {
    getData() {
      info({
        article_id: this.info.article_id,
      }).then((res) => {
        let dataInfo = res.data.info;
        this.submitForm = {
          article_id: dataInfo.article_id,
          title: dataInfo.title,
          content: dataInfo.content,
          sort: 0,
        };
      });
    },
    // 提交
    handleSubmit() {
      this.$refs.submitFormRef.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.isLoading.submit = true;

        edit(this.submitForm)
          .then(() => {
            this.isLoading.submit = false;
            this.$emit("close");
          })
          .catch(() => {
            this.isLoading.submit = false;
          });
      });
    },
    // 关闭
    handleClose() {
      this.$emit("close", "cancel");
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-textarea textarea {
  font-family: "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}

::v-deep .tox-tinymce {
  min-height: 500px;
}

.image-box {
  width: 100%;
  height: 100%;
}
</style>
